var conf = require('../../conf');
var TABLE = require('./table');

var knex = require('knex')({
    client: 'mysql',
    connection: {
        filename: "./mydb.sqlite",
        host: conf.mysql.host,
        user: conf.mysql.user,
        password: conf.mysql.password,
        database: conf.mysql.database.myapp,
    },
    pool: {min: 0, max: 7}
});

knex.schema.hasTable(TABLE.ballshooter.tableName).then(function (exists) {
    if (!exists) {
        knex.schema.createTable(TABLE.ballshooter.tableName, function (table) {
            table.string(TABLE.ballshooter.column.key, 64);
            table.string(TABLE.ballshooter.column.value, 1024).notNullable();
            table.string(TABLE.ballshooter.column.tag, 64).primary();
            table.timestamp(TABLE.ballshooter.column.update_at);
            table.primary([TABLE.ballshooter.column.key, TABLE.ballshooter.column.value]);
        }).asCallback();
    }
});

module.exports = knex;